package com.mc.moring.widge;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.mc.moring.BafBase.NArrayAdapter;
import com.mc.moring.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 账户
 */
public class JPayDialog extends DialogFragment {

    public interface DialogCallback {

        /**
         * 列表选定回调
         *
         * @param selectedData 选定的列表项数据
         */
        void onConfirm(String selectedData);
    }

    private static Context mContext;
    private static JPayDialog dialog;

    private static DialogCallback callback;

    private ImageView btnCancel;
    private TextView btnConfirm;
    private ListView listView;
    private static ArrayList<String> mListData;
    private String selectedData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_jh_pay, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = view.findViewById(R.id.listView);
        btnCancel = view.findViewById(R.id.btn_close);
        btnConfirm = view.findViewById(R.id.btn_confirm);
    }

    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        MyAdapter adapter = new MyAdapter(getActivity(), mListData);

        listView.setOnItemClickListener((arg01, view, position, arg3) -> {
            adapter.setSelectPosition(position);
            selectedData = mListData.get(position);
        });

        btnConfirm.setOnClickListener(view -> {
            callback.onConfirm(selectedData);
            dialog.dismiss();
        });

        btnCancel.setOnClickListener(view -> dialog.dismiss());

        listView.setAdapter(adapter);
    }


    /**
     * 创建并显示对话框。
     *
     * @param act
     * @param mCallback
     */
    public static JPayDialog show(FragmentActivity act, List<String> listData, DialogCallback mCallback) {
        mListData.addAll(listData);
        mContext = act.getApplicationContext();

        FragmentTransaction ft = act.getSupportFragmentManager().beginTransaction();
        Fragment prev = act.getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create the dialog.
        dialog = new JPayDialog();
        dialog.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog_Them);

//		//按空白处不能取消动画
//		dialog.setCanceledOnTouchOutside(true);
//		//设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
//		dialog.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//		//一定要在setContentView之后调用，否则无效
//		dialog.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


        dialog.show(ft, "dialog_pay");

        callback = mCallback;

        return dialog;

    }

    public class MyAdapter extends NArrayAdapter {

        private int defaultSelect = 0;

        /**
         * 构造函数
         *
         * @param context 上下文
         * @param data    数据
         */
        MyAdapter(Context context, ArrayList<String> data) {
            super(context, data);
        }

        @Override
        public View getListItemView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_jh_pay, null, false);
                viewHolder = new ViewHolder();

                // 初始化空间
                viewHolder.itemDataTv = convertView.findViewById(R.id.tv_name);
                viewHolder.select = convertView.findViewById(R.id.iv_select);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            // 设置数据
            if (mData != null && !mData.isEmpty()) {
                viewHolder.itemDataTv.setText(mData.get(position).toString());
            }

            if (position == defaultSelect) {
                viewHolder.select.setImageResource(R.drawable.ic_radio_selected);
            } else {
                viewHolder.select.setImageResource(R.drawable.ic_radio_normal);
            }
            return convertView;
        }

        class ViewHolder {
            TextView itemDataTv;
            ImageView select;
        }

        public void setSelectPosition(int position) {
            if (!(position < 0 || position > mData.size())) {
                defaultSelect = position;
                notifyDataSetChanged();
            }
        }

        public int getSelectPosition() {
            return defaultSelect;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                int width = ViewGroup.LayoutParams.MATCH_PARENT;
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                window.setLayout(width, height);
            }
        }
    }
}
